<template>
  <a-modal
    :title="title"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="_submit"
    @cancel="_cancel">
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['roleId', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="角色名称">
          <a-input v-decorator="['roleName', { rules: [{ required: true, message: '角色名称不能为空!' }] }]" />
        </a-form-item>
        <a-form-item label="描述">
          <a-textarea
            v-decorator="['description', { rules: [{ required: true, message: '请输入至少五个字符的规则描述！' }] }]"
          />
        </a-form-item>
        <a-form-item label="状态">
          <a-select
            placeholder="请选择状态"
            v-decorator="['status', { rules: [{ required: true, message: '请选择状态,状态不能为空！' }] }]"
          >
            <a-select-option :value="1">启用</a-select-option>
            <a-select-option :value="2">禁用</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="权限树">
          <a-tree
            ref="tree"
            node-key="permissionId"
            checkable
            :tree-data="treeData"
            :checkedKeys="checkedKeys"
            :replace-fields="replaceFields"
            @check="_check"
            v-decorator="['permissionList', { rules: [{ required: true, message: '请选择权限,权限不能为空！' }] }]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { getPermissionList } from '@/api/common'

// 表单字段
const fields = ['roleId', 'roleName', 'description', 'status', 'permissionList']

export default {
  props: {
    title: {
      type: String,
      required: true
    },
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    checkedKeys: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      treeData: [],
      replaceFields: {
        key: 'permissionId',
        title: 'permissionName'
      },
      form: this.$form.createForm(this)
    }
  },
  methods: {
    /**
     * 选择权限
     */
    _check: function (checkedKeys, evt) {
      this.$emit('setCheckedKeys', checkedKeys)
      this.setFormData()
    },
    /**
     * 提交表单
     */
    _submit: function () {
      this.$emit('ok')
    },
    /**
     * 取消
     */
    _cancel: function () {
      this.$emit('cancel')
    },
    /**
     * 设值form.permissionList
     */
    setFormItem: function () {
      this.$nextTick(() => {
        const permissionList = this.checkedKeys.map((item) => {
          return {
            permissionId: item
          }
        })
        this.form.setFieldsValue({
          permissionList
        })
      })
    }
  },
  watch: {
    checkedKeys: function (newVal, oldVal) {
      this.setFormItem()
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  mounted () {
    getPermissionList({})
      .then((res) => {
        this.treeData = res.permissionList
      })
      .catch((e) => {})
  }
}
</script>
